#include "ScreenPresentation.h"
#include "ui_ScreenPresentation.h"
#include <QScrollBar>

ScreenPresentation::ScreenPresentation(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ScreenPresentation),
	_screenView(NULL)
{
	ui->setupUi(this);

	scene.setItemIndexMethod(QGraphicsScene::NoIndex);

	ui->graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
	ui->graphicsView->setScene(&scene);
	ui->graphicsView->setRenderHint(QPainter::Antialiasing);
	ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);
	ui->graphicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
	ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
	ui->graphicsView->viewport()->setCursor(Qt::ArrowCursor);
	//QApplication::setOverrideCursor( Qt::ArrowCursor );
	ui->graphicsView->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
	ui->graphicsView->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );

	_selectable = false;

	initScene();

	_CW = true;
	_startIndex = 0;
	_ledsW = 4;
	_ledsH = 4;
	_ledsCount = -1;
	reloadLeds();
}

ScreenPresentation::~ScreenPresentation()
{
	delete ui;
}

void ScreenPresentation::update1()
{
	scene.update();
}

void ScreenPresentation::setDirection(bool clockwise)
{
    if(_CW != clockwise){
        for(int i=0; i<_ledsList.size()/2; i++){
            _ledsList.swap(i, _ledsList.size() - (1+i) );
        }
        _startIndex = _ledsList.size() - _startIndex;
    }
	_CW = clockwise;

}

void ScreenPresentation::setStartLed(LedView* led)
{
	int num = 0;
	foreach(LedView* ledView, _ledsList){
		if(ledView == led){
			_startIndex = num;
			return;
		}
		if(ledView->isLedActive()) num++;
	}
}

void ScreenPresentation::setLedsCount(int x, int y)
{
	if(y == -1){
		_ledsCount = x;
		_ledsW = -1;
		_ledsH = -1;
	}
	else{
		_ledsCount = -1;
		_ledsW = x;
		_ledsH = y;
	}
	reloadLeds();
}

QList<LedView*> ScreenPresentation::getLeds()
{
	return _ledsList;
}

void ScreenPresentation::initScene(){
	_screenView = new ScreenView(0,0,100,50);
	scene.addItem(_screenView);
}

void ScreenPresentation::updateLeds(QList<LedStripe> stripes)
{
	int count = _ledsList.count();
	int globalNum = 0;

	double XPart = 0, YPart = 0;
	foreach(LedStripe stripe, stripes){
		if(stripe.direction == 0 || stripe.direction == 180) YPart += stripe.percent;
		else XPart += stripe.percent;
	}

    int sum=0;

	foreach(LedStripe stripe, stripes){

		int num = 0;
		if(_ledsCount != -1){
            num = (count * stripe.percent / 100 + 0.5); sum+=num;
			if(stripe.start == stripes.last().start) num = count - globalNum;
		}
		else{
			if(stripe.direction == 0 || stripe.direction == 180){
				num = 2*_ledsH * stripe.percent / YPart;
			}
			else{
				num = 2*_ledsW * stripe.percent / XPart;
			}
		}

		double dx = (double)(stripe.end.x() - stripe.start.x()) / (num-1);
		double dy = (double)(stripe.end.y() - stripe.start.y()) / (num-1);
		int middleX = (stripe.end.x() + stripe.start.x()) / 2;
		int middleY = (stripe.end.y() + stripe.start.y()) / 2;
		for(int i=0;i<num;i++,globalNum++){
			_ledsList.at(globalNum)->setDirection(stripe.direction);
            _ledsList.at(globalNum)->setRadius(_screenView->boundingRect().width()/10/_screenView->getScreenNumber());
			if(num==1){
				_ledsList.at(globalNum)->setPosition(middleX, middleY);
			}
			else{
				_ledsList.at(globalNum)->setPosition(stripe.start.x() + i*dx, stripe.start.y() + i*dy);
			}
		}
    }
}

bool ScreenPresentation::reloadLeds(){
	// remove leds
	for(int i=0;i<_ledsList.count();i++){
		scene.removeItem(_ledsList.at(i));
		//_ledsList.at(0);
	}
	_ledsList.clear();


	for(int i=0;i< (_ledsCount == -1 ? 2*_ledsH+2*_ledsW : _ledsCount);i++){
		LedView* led = new LedView(0,0,40,0,80,this);
		led->setMainWidget(this);
		led->setColor(_selectable ? QColor(213,34,45) : QColor(240,240,240));
		led->setSelectable(_selectable);
		_ledsList.append(led);
		scene.addItem(led);
	}

	if(_screenView){
		_screenView->setGeometry(width(),height());
		updateLeds(_screenView->stripes());
		scene.setSceneRect(0,0,width(),height());
	}

	if(_selectable) updateStartLed();
}


void ScreenPresentation::resizeEvent(QResizeEvent * event){
	if(_screenView){
		_screenView->setGeometry(width(),height());
		updateLeds(_screenView->stripes());
		scene.setSceneRect(0,0,width(),height());
	}
}

void ScreenPresentation::updateStartLed()
{
	int num = 0;
	for(int i=0;i<_ledsList.count();i++){
		_ledsList.at(i)->setStart(false);
		_ledsList.at(i)->setColor(Qt::red);
	}
	for(int i=0;i<_ledsList.count();i++){
		LedView* led = _ledsList.at(i);
		if(num == _startIndex && led->isLedActive()){
			led->setStart(true);
			led->setColor(Qt::blue);
			return;
		}
		if(led->isLedActive()) num++;
	}
	_startIndex = 0;
	_ledsList.at(0)->setStart(true);
	_ledsList.at(0)->setColor(Qt::blue);
}

// obsolete
/*void ScreenPresentation::updateLeds(QRectF rect){
	int dx = rect.width()/(_ledsW-1);
	int dy = rect.height()/(_ledsH-1);
	foreach(LedView* led, _ledsList){
		led->setPosition(rect.x() + led->getGridPos().x()*dx,rect.y() + led->getGridPos().y()*dy);
	}
}

int ScreenPresentation::posToCount(int x, int y, bool CW){
	if(y==0) return x;
	else if(x==_ledsW-1) return _ledsW + y;
	else if(y==_ledsH-1) return _ledsW + _ledsH + (_ledsW-1-x);
	else return _ledsW*_ledsH - y;
	return 0;
}

QPoint ScreenPresentation::countToPos(int cnt, bool CW){
	int cnts[5] = {0,  _ledsW, _ledsW+_ledsH, 2*_ledsW+_ledsH, 2*_ledsW+2*_ledsH};
	if(cnt >= cnts[0] && cnt < cnts[1]) return QPoint(cnt,0); //top
	if(cnt >= cnts[1] && cnt < cnts[2]) return QPoint(_ledsW-1,cnt-cnts[1]); //right
	if(cnt >= cnts[2] && cnt < cnts[3]) return QPoint(cnt - cnts[2],_ledsH-1); //bottom
	if(cnt >= cnts[3]) return QPoint(0,cnts[4]-cnt); //left
	return QPoint(0,0);
}*/
